<?php

namespace app\common\model\banxi\core;

use app\common\enums\AppointmentStatusEnum;
use app\common\enums\CoreCourseTypeEnum;
use app\common\enums\CourseDeliveryTypeEnum;
use app\common\enums\CourseFullTypeEnum;
use app\common\model\banxi\learn\LearnSkRecord;
use app\common\model\banxi\organization\Organization;
use app\common\model\banxi\say\SayTeachSentence;
use app\common\model\banxi\student\Student;
use app\common\model\banxi\trainer\Trainer;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 排课预约表
 */
class CoreCourseAppointment extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_core_course_appointment';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'appointmentStatusCn',
        'courseDeliveryTypeCn',
        'courseFullTypeCn',
        'courseTypeCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'courseId' => 'string',
        'studentId' => 'string',
        'trainerId' => 'string',
        'isKaiKeMsgSend' => 'boolean',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getAppointmentStatusCnAttr($value, $data)
    {
        $value = $value ?: ($data['appointmentStatus'] ?? '');
        return (new AppointmentStatusEnum())->getDescription($value) ?? '';
    }

    public function getCourseDeliveryTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['courseDeliveryType'] ?? '');
        return (new CourseDeliveryTypeEnum())->getDescription($value) ?? '';
    }

    public function getCourseFullTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['courseFullType'] ?? '');
        return (new CourseFullTypeEnum())->getDescription($value) ?? '';
    }

    public function getCourseTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['courseType'] ?? '');
        return (new CoreCourseTypeEnum())->getDescription($value) ?? '';

    }

    //-----------------------------------------------------------------------------------
    //｜                                  处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    public function getListPage(array $where, string $page = "1", int $limit = 10)
    {
        return $this->with([
            'studentInfo',
            'trainerInfo',
            'organizationBaseInfo' => function ($query) {
                $query->field('id,name,managerName');
            }])->order(['id' => 'desc'])->where($where)->paginate(['page' => $page, 'list_rows' => $limit])->each(function ($item) {
            if ($item['studentOrganizationId'] == 0) {
                $organization = new Organization();
                $item['organizationBaseInfo'] = $organization->getOrganizationInfo();
            }
        });
    }

    public function getInfo(array $where)
    {
        $info = $this->with([
            'studentInfo',
            'trainerInfo',
            'organizationBaseInfo' => function ($query) {
                $query->field('id,name,managerName');
            }])->where($where)->find();
        if (empty($info)) {
            return [];
        }
        if ($info['studentOrganizationId'] == 0) {
            $organization = new Organization();
            $info['organizationBaseInfo'] = $organization->getOrganizationInfo();
        }
        return $info;
    }



    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------
    public function organizationBaseInfo()
    {
        return $this->hasOne(Organization::class, 'id', 'studentOrganizationId');
    }

    public function studentInfo()
    {
        return $this->hasOne(Student::class, 'id', 'studentId');
    }

    public function trainerInfo()
    {
        return $this->hasOne(Trainer::class, 'id', 'trainerId');
    }

    public function courseInfo()
    {
        return $this->hasOne(SayTeachSentence::class, 'id', 'courseId');
    }

    public function skRecordInfo()
    {
        return $this->hasOne(LearnSkRecord::class, 'appointmentId', 'id');

    }

}